package cdu.bbs.bbsv2.service.impl;

import cdu.bbs.bbsv2.mapper.TopicMapper;
import cdu.bbs.bbsv2.mapper.UserMapper;
import cdu.bbs.bbsv2.domain.User;
import cdu.bbs.bbsv2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
    //UserMapper userMapper = new UserMapperImpl();
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public User login(String loginAct, String loginPwd) {
        return userMapper.login(loginAct,loginPwd);
    }

    @Override
    public boolean register(User user) {
        return userMapper.register(user)==1?true:false;
    }

    @Override
    public List<User> getUsers() {
        return userMapper.getUsers();
    }

    @Override
    public Boolean deleteUserById(String id) {
        boolean flag = userMapper.deleteUserById(id)==1;
        if (flag)
            if (1!=topicMapper.deleteTopicByUserId(id)){
                flag = false;
            }
        return flag;
    }

    @Override
    public User getUserById(String id) {
        return userMapper.getUserById(id);
    }

    @Override
    public boolean modUserById(User user) {
        return userMapper.modUserById(user)==1?true:false;
    }

    @Override
    public List<User> getUsersByCondition(Map<String, String> conditions) {
        return userMapper.getUserByCondition(conditions);
    }

    @Override
    public Boolean deleteUserByIds(String[] ids) {
        boolean flag = false;
        flag = userMapper.deleteUserByIds(ids)==ids.length;
        if (flag){
            for (String id : ids) {
                if(topicMapper.deleteTopicByUserId(id) != 1){
                    flag=false;
                };
            }
        }
        return flag;
    }


}
